/**
 * Created by liuzhen on 2017/2/27.
 */
define(["globalize","jqxcore","jqxbuttons","jqxscrollbar","jqxdropdownlist","jqxgrid","jqxgrid-filter",
    "jqxgrid-sort","jqxgrid-selection","jqxgrid-edit","jqxgrid-pager","jqxgrid-aggregates","generatedata"],function () {
    var theme = "light";
    var localizationobj = {};
    localizationobj.pagergotopagestring = "当前页:";
    localizationobj.pagershowrowsstring = "每页显示:";
    localizationobj.pagerrangestring = " 总共 ";
    localizationobj.pagernextbuttonstring = "后页";
    localizationobj.pagerpreviousbuttonstring = "前页";
    localizationobj.sortascendingstring = "正序";
    localizationobj.sortdescendingstring = "倒序";
    localizationobj.sortremovestring = "清除排序";
    localizationobj.firstDay = 1;
    localizationobj.percentsymbol = "%";
    localizationobj.currencysymbol = "￥";
    localizationobj.currencysymbolposition = "before";
    localizationobj.decimalseparator = ".";
    localizationobj.thousandsseparator = ",";
    // apply localization.
    var createGrid = function () {
        var data = generatedata(500);
        var source =
        {
            localdata: data,
            datafields: [
                {name: 'firstname', type: 'string'},
                {name: 'lastname', type: 'string'},
                {name: 'productname', type: 'string'},
                {name: 'date', type: 'date'},
                {name: 'quantity', type: 'number'}
            ],
            datatype: "array",
            //pagenum: 3,
            //pagesize: 50,
            //pagesizeoptions:[50,100,200],
        };

        var addfilter = function () {
            var filtergroup = new $.jqx.filter();

            var filter_or_operator = 1;
            var filtervalue = 'Beate';
            var filtercondition = 'contains';
            var filter1 = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);

            filtervalue = 'Andrew';
            filtercondition = 'contains';
            var filter2 = filtergroup.createfilter('stringfilter', filtervalue, filtercondition);

            filtergroup.addfilter(filter_or_operator, filter1);
            filtergroup.addfilter(filter_or_operator, filter2);
            // add the filters.
            $("#jqxgrid").jqxGrid('addfilter', 'firstname', filtergroup);
            // apply the filters.
            $("#jqxgrid").jqxGrid('applyfilters');
        }
        var dataAdapter = new $.jqx.dataAdapter(source);

            $("#jqxgrid").jqxGrid({
                'localizestrings': localizationobj,
                //theme:"custom",
                width: $(".resizable-frame").width() - 2,
                height: $(".resizable-frame").height(),
                source: dataAdapter,
                pageable: true,
                pagesize: 50,
                pagesizeoptions: [50, 100, 200],
                filterable: true,
                sortable: true,
                editable: true,
                altrows: true,
                autoshowfiltericon: true,
                //pagermode: 'simple',
                statusbarheight: 50,
                showaggregates: true,
                selectionmode: 'multiplecellsadvanced',
                ready: function () {
                    //addfilter();
                    var localizationObject = {
                        filterstringcomparisonoperators: ['contains', 'does not contain'],
                        // filter numeric comparison operators.
                        filternumericcomparisonoperators: ['less than1', 'greater than'],
                        // filter date comparison operators.
                        filterdatecomparisonoperators: ['less than', 'greater than'],
                        // filter bool comparison operators.
                        filterbooleancomparisonoperators: ['equal', 'not equal']
                    }
                    $("#jqxgrid").jqxGrid('localizestrings', localizationObject);
                },
                updatefilterconditions: function (type, defaultconditions) {
                    var stringcomparisonoperators = ['CONTAINS', 'DOES_NOT_CONTAIN'];
                    var numericcomparisonoperators = ['LESS_THAN', 'GREATER_THAN'];
                    var datecomparisonoperators = ['LESS_THAN', 'GREATER_THAN'];
                    var booleancomparisonoperators = ['EQUAL', 'NOT_EQUAL'];
                    switch (type) {
                        case 'stringfilter':
                            return stringcomparisonoperators;
                        case 'numericfilter':
                            return numericcomparisonoperators;
                        case 'datefilter':
                            return datecomparisonoperators;
                        case 'booleanfilter':
                            return booleancomparisonoperators;
                    }
                },
                updatefilterpanel: function (filtertypedropdown1, filtertypedropdown2, filteroperatordropdown, filterinputfield1, filterinputfield2, filterbutton, clearbutton,
                                             columnfilter, filtertype, filterconditions) {
                    var index1 = 0;
                    var index2 = 0;
                    if (columnfilter != null) {
                        var filter1 = columnfilter.getfilterat(0);
                        var filter2 = columnfilter.getfilterat(1);
                        if (filter1) {
                            index1 = filterconditions.indexOf(filter1.comparisonoperator);
                            var value1 = filter1.filtervalue;
                            filterinputfield1.val(value1);
                        }

                        if (filter2) {
                            index2 = filterconditions.indexOf(filter2.comparisonoperator);
                            var value2 = filter2.filtervalue;
                            filterinputfield2.val(value2);
                        }
                    }

                    filtertypedropdown1.jqxDropDownList({autoDropDownHeight: true, selectedIndex: index1});
                    filtertypedropdown2.jqxDropDownList({autoDropDownHeight: true, selectedIndex: index2});
                },
                columns: [
                    {text: '姓', datafield: 'firstname', width: 200},
                    {text: '名', datafield: 'lastname', width: 200},
                    {text: '产品', datafield: 'productname', width: 180},
                    {text: '下单日期', datafield: 'date', width: 160, cellsformat: 'dd-MMMM-yyyy'},
                    {text: '数量', datafield: 'quantity', cellsalign: 'right'}
                ]
            });

        $('#events').jqxPanel({width: 300, height: 80});

        $("#jqxgrid").on("filter", function (event) {
            $("#events").jqxPanel('clearcontent');
            var filterinfo = $("#jqxgrid").jqxGrid('getfilterinformation');

            var eventData = "Triggered 'filter' event";
            for (i = 0; i < filterinfo.length; i++) {
                var eventData = "Filter Column: " + filterinfo[i].filtercolumntext;
                $('#events').jqxPanel('prepend', '<div style="margin-top: 5px;">' + eventData + '</div>');
            }
        });

        $('#clearfilteringbutton').jqxButton({theme: theme});
        $('#filterbackground').jqxCheckBox({checked: true, height: 25});
        $('#filtericons').jqxCheckBox({checked: false, height: 50});
        // clear the filtering.
        $('#clearfilteringbutton').click(function () {
            $("#jqxgrid").jqxGrid('clearfilters');
        });
        // show/hide filter background
        $('#filterbackground').on('change', function (event) {
            $("#jqxgrid").jqxGrid({showfiltercolumnbackground: event.args.checked});
        });
        // show/hide filter icons
        $('#filtericons').on('change', function (event) {
            $("#jqxgrid").jqxGrid({autoshowfiltericon: !event.args.checked});
        });
    }
    function init() {
        createGrid()
    };
    return init();
});